package com.smile67.controller;


import cn.hutool.core.bean.BeanUtil;
import com.smile67.common.restult.PageResult;
import com.smile67.common.restult.Result;
import com.smile67.pojo.dto.DeviceManagementDTO;
import com.smile67.pojo.dto.DeviceManagementPageQueryDTO;
import com.smile67.pojo.entity.DeviceManagement;
import com.smile67.service.IDeviceManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/device")
public class DeviceManagementController {
    @Autowired
    private IDeviceManagementService deviceManagementService;

    /**
     * 新增设备管理
     */
    @PostMapping
    public Result<String> save(@RequestBody DeviceManagementDTO deviceManagementDTO) {
        DeviceManagement deviceManagement = BeanUtil.copyProperties(deviceManagementDTO, DeviceManagement.class);
        deviceManagementService.save(deviceManagement);
        return Result.success("添加成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids) {
        deviceManagementService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }

    /**
     * 修改
     *
     * @param deviceManagementDTO
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody DeviceManagementDTO deviceManagementDTO) {
        DeviceManagement deviceManagement = BeanUtil.copyProperties(deviceManagementDTO, DeviceManagement.class);
        deviceManagementService.updateById(deviceManagement);
        return Result.success("修改成功");
    }

    /**
     * 查单个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DeviceManagement> getById(@PathVariable Long id) {
        DeviceManagement deviceManagement = deviceManagementService.getById(id);
        return Result.success(deviceManagement);
    }

    /**
     * 分页查询
     *
     * @param deviceManagementPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(DeviceManagementPageQueryDTO deviceManagementPageQueryDTO) {
        PageResult pageResult = deviceManagementService.pageQuery(deviceManagementPageQueryDTO);
        return Result.success(pageResult);
    }

}
